home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / gfx / conv / gfx2grob.lha / gfx2grob / src / Convert.c next >
C/C++ Source or Header  |  1994-11-20  |  6KB  |  247 lines

  1. /**************************************************************************
  2.  
  3.             Convert bin -> ascii and vice versa
  4.  
  5.        Copyright (C) 1994 by Alexandros Loghis,  All Rights Reserved
  6.  
  7. **************************************************************************/
  8.  
  9. #include <stdio.h>    /* fprintf, fgets, fopen, fgetc, fputc, feof \
  10.                remove */
  11. #include <stdlib.h>    /* atoi */
  12. #include <string.h>    /* strcmp */
  13.  
  14. #include "gfx2grob.h"
  15. #include "PrintMsg.h"
  16. #include "FileStuf.h"
  17. #ifdef AMIGA
  18. #include "IffStuff.h"
  19. #endif
  20. #include "Convert.h"
  21.  
  22.  
  23. #define HPLINE        "%%%%HP: T(2)A(R)F(.);" /* first line in ASCII-file */
  24. #define HPLINELEN    sizeof(HPLINE)
  25. #define GROBSTRLEN    sizeof(GROBSTR)
  26. #define HPALIGN        8
  27. #define COMPALIGN    16
  28.  
  29.  
  30. u_char ConvertTab[] = { 0x0, 0x8, 0x4, 0xC,
  31.             0x2, 0xA, 0x6, 0xE,
  32.             0x1, 0x9, 0x5, 0xD,
  33.             0x3, 0xB, 0x7, 0xF
  34.               };
  35.  
  36.  
  37. static int GetAndConvNibble(gvType *gv);
  38. static char *GetWord(gvType *gv, char *Buffer, int MaxLen);
  39. static void ConvertFileToBin(gvType *gv);
  40. static void ConvAndPutNibble(gvType *gv, int Index);
  41. static void ConvertFileToAscii(gvType *gv);
  42. #ifdef AMIGA
  43. static void ConvertFileToIff(gvType *gv);
  44. #endif
  45.  
  46. /*************************************************************************/
  47.  
  48. static int GetAndConvNibble(gvType *gv)
  49. {
  50.   int x;
  51.  
  52.  
  53.   if (EOF != (x = fgetc(gv->Inputfile))) {
  54.     if (x > '9')
  55.       x -= 'A' - 10;
  56.     else
  57.       x -= '0';
  58.   }
  59.   else
  60.     PrintMsg(ERR_READ_S, gv->Options.InputfileName);
  61.  
  62.   return ((int) ConvertTab[x]);
  63. }
  64.  
  65. /*************************************************************************/
  66.  
  67. static char *GetWord(gvType *gv, char *Buffer, int MaxLen)
  68. {
  69.   int x;
  70.  
  71.   char *b = Buffer;
  72.  
  73.  
  74.   while ((x = fgetc(gv->Inputfile)) != ' ') {
  75.     if (x == EOF) PrintMsg(ERR_READ_S, gv->Options.InputfileName);
  76.     if (!--MaxLen) PrintMsg(ERR_WORD_S, gv->Options.InputfileName);
  77.     *Buffer++ = x;
  78.   }
  79.   *Buffer = '\0';
  80.  
  81.   return (b);
  82. }
  83.  
  84. /*************************************************************************/
  85.  
  86. static void ConvertFileToBin(gvType *gv)
  87. {
  88.   char HPline[HPLINELEN + 1],
  89.        Grob[GROBSTRLEN + 1],
  90.        Width[5],
  91.        Height[5];
  92.  
  93.   int Wi,
  94.       Hi,
  95.       HPWidth,
  96.       CompWidth,
  97.       i,
  98.       j;
  99.  
  100.   register int hn,    /* high nibble */
  101.            ln;    /* low    nibble */
  102.  
  103.  
  104.   PrintMsg(MSG_SKIPLN);
  105.   if (!fgets(HPline, HPLINELEN + 2, gv->Inputfile))
  106.     PrintMsg(ERR_READ_S, gv->Options.InputfileName);
  107.  
  108.   if (strcmp(GetWord(gv, Grob, GROBSTRLEN), GROBSTR))
  109.     PrintMsg(ERR_GROB_S, gv->Options.InputfileName);
  110.   Wi = atoi(GetWord(gv, Width, 4));
  111.   Hi = atoi(GetWord(gv, Height, 4));
  112.   PrintMsg(MSG_GPHFORMIS_DD, Wi, Hi);
  113.   gv->Options.Width = Wi;
  114.   gv->Options.Height  = Hi;
  115.   HPWidth   = CALCWIDTH(Wi, HPALIGN);
  116.   CompWidth = CALCWIDTH(Wi, COMPALIGN);
  117.  
  118.   for (i = 0; i < Hi; i++) {
  119.     for (j = 0; j < (HPWidth / HPALIGN); j++) {
  120.       hn = GetAndConvNibble(gv);
  121.       ln = GetAndConvNibble(gv);
  122.       if (EOF == fputc((hn << 4) + ln, gv->Outputfile))
  123.     PrintMsg(ERR_WRITE_S, gv->Options.OutputfileName);
  124.     }
  125.     for (j = 0; j < (CompWidth - HPWidth) / HPALIGN; j++)
  126.       if (EOF == fputc(0, gv->Outputfile))
  127.       PrintMsg(ERR_WRITE_S, gv->Options.OutputfileName);
  128.   }
  129.   fgetc(gv->Inputfile);        /* last CR */
  130.   fgetc(gv->Inputfile);        /* to come to EOF */
  131.   if (!feof(gv->Inputfile)) PrintMsg(ERR_EOF_S, gv->Options.InputfileName);
  132. }
  133.  
  134. /*************************************************************************/
  135.  
  136. static void ConvAndPutNibble(gvType *gv, int Index)
  137. {
  138.   int y;
  139.  
  140.  
  141.   if ((y = ConvertTab[Index]) > 9)
  142.     y += 'A' - 10;
  143.   else
  144.     y += '0';
  145.   if (EOF == fputc(y, gv->Outputfile))
  146.     PrintMsg(ERR_WRITE_S, gv->Options.OutputfileName);
  147. }
  148.  
  149. /*************************************************************************/
  150.  
  151. static void ConvertFileToAscii(gvType *gv)
  152. {
  153.   int HPWidth,
  154.       CompWidth,
  155.       i,
  156.       j;
  157.  
  158.   register int x;
  159.  
  160.  
  161.   if (0 > fprintf(gv->Outputfile, HPLINE"\n"GROBSTR" %d %d ",
  162.           gv->Options.Width, gv->Options.Height))
  163.     PrintMsg(ERR_WRITE_S, gv->Options.OutputfileName);
  164.  
  165.   PrintMsg(MSG_GPHFORMIS_DD, gv->Options.Width, gv->Options.Height);
  166.  
  167.   HPWidth   = CALCWIDTH(gv->Options.Width, HPALIGN);
  168.   CompWidth = CALCWIDTH(gv->Options.Width, COMPALIGN);
  169.  
  170.   for (i = 0; i < gv->Options.Height; i++) {
  171.     for (j = 0; j < (HPWidth / HPALIGN); j++)
  172.       if (EOF != (x = fgetc(gv->Inputfile))) {
  173.     ConvAndPutNibble(gv, x >> 4);
  174.     ConvAndPutNibble(gv, x & 0x0F);
  175.       }
  176.       else
  177.     PrintMsg(ERR_READ_S, gv->Options.InputfileName);
  178.     for (j = 0; j < (CompWidth - HPWidth) / HPALIGN; j++)
  179.       if (EOF == fgetc(gv->Inputfile))
  180.     PrintMsg(ERR_READ_S, gv->Options.InputfileName);
  181.   }
  182.   fgetc(gv->Inputfile);        /* to come to EOF */
  183.   if (!feof(gv->Inputfile))
  184.     PrintMsg(ERR_EOF_S, gv->Options.InputfileName);
  185. }
  186.  
  187. /*************************************************************************/
  188.  
  189. #ifdef AMIGA
  190. static void ConvertFileToIff(gvType *gv)
  191. {
  192.   char *OutputfileNameTmp = gv->Options.OutputfileName;
  193.  
  194.  
  195.   if (EOF == fclose(gv->Outputfile))
  196.     PrintMsg(ERR_CLOSE_S, OutputfileNameTmp);
  197.  
  198.   gv->Options.OutputfileName = TEMPFILENAME;
  199.   if (!(gv->Outputfile = fopen(TEMPFILENAME, "wb")))
  200.     PrintMsg(ERR_OPEN_S, TEMPFILENAME);
  201.  
  202.   ConvertFileToBin(gv);
  203.  
  204.   if (EOF == fclose(gv->Inputfile))
  205.     PrintMsg(ERR_CLOSE_S, gv->Options.InputfileName);
  206.   gv->Inputfile = NULL;
  207.   if (EOF == fclose(gv->Outputfile))
  208.     PrintMsg(ERR_CLOSE_S, gv->Options.OutputfileName);
  209.   gv->Outputfile = NULL;
  210.  
  211.   gv->Options.InputfileName  = TEMPFILENAME;
  212.   gv->Options.OutputfileName = OutputfileNameTmp;
  213.  
  214.   ConvertBinToIff(gv);
  215. }
  216. #endif
  217.  
  218. /*************************************************************************/
  219.  
  220. extern void ConvertFile(gvType *gv)
  221. {
  222.   switch (gv->Options.Option) {
  223.  
  224.     case OT_TOASCII :
  225.       OpenFiles(gv, "rb", "w");
  226. #ifdef AMIGA
  227.       IffToBin(gv);
  228. #endif
  229.       ConvertFileToAscii(gv);
  230.       break;
  231.  
  232.     case OT_TOBIN :
  233.       OpenFiles(gv, "r", "wb");
  234.       ConvertFileToBin(gv);
  235.       break;
  236.  
  237. #ifdef AMIGA
  238.     case OT_TOIFF :
  239.       OpenFiles(gv, "r", "wb");
  240.       ConvertFileToIff(gv);
  241.       break;
  242. #endif
  243.   }
  244.   CloseFiles(gv);
  245.   PrintMsg(MSG_OPSUCCESS);
  246. }
  247.